﻿using Abp.Domain.Repositories;
using Abp.EntityFramework;
using Abp.ObjectMapping;
using AVIS.OutBound.EntityFramework.Models;
using AVIS.OutBound.Wizard;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.OutBound.EntityFramework.Repositories
{
    public class WizardRepository : OutBoundRepositoryBase<AvisErrorMessage>, IWizardRepository
    {
        private readonly IRepository<AvisErrorMessage> _AvisErrorMessageRepository;
        private readonly IRepository<avisPriceInclude> _avisPriceIncludeRepository;
        private readonly IRepository<tblMasterData> _masterDataRepository;
        private readonly IObjectMapper _objectMapper;
        public WizardRepository(IDbContextProvider<OutBoundDbContext> dbContextProvider,
            IRepository<AvisErrorMessage> AvisErrorMessageRepository,
            IRepository<avisPriceInclude> avisPriceIncludeRepository,
            IRepository<tblMasterData> masterDataRepository,
            IObjectMapper objectMapper) : base(dbContextProvider)
        {
            _AvisErrorMessageRepository = AvisErrorMessageRepository;
            _avisPriceIncludeRepository = avisPriceIncludeRepository;
            _masterDataRepository = masterDataRepository;
            _objectMapper = objectMapper;
        }

        /// <summary>
        /// 获取指定代码的错误信息
        /// </summary>
        /// <param name="ErrorCode"></param>
        /// <returns></returns>
        public ErrorMessage GetErrorMessage(string ErrorCode)
        {
            var data = _AvisErrorMessageRepository.FirstOrDefault(c => c.ErrorCode == ErrorCode);
            var Error = _objectMapper.Map<ErrorMessage>(data);
            return Error;
        }

        /// <summary>
        /// 获取所有价格包含（PriceInclude）的中文解释
        /// </summary>
        /// <returns></returns>
        public IDictionary<string, string> GetAllPriceIncludes()
        {
            var dicPriceInclude = _avisPriceIncludeRepository.GetAll()
                        .Select(p => new { p.IncludeEN, p.IncludeZH })
                        .ToDictionary(p => p.IncludeEN, p => p.IncludeZH);
            return dicPriceInclude;
        }

        /// <summary>
        /// 获取所有价格包含（PriceInclude）的简写中文解释
        /// 注意：+号表示一条内容里面同时包含关键字，有 +号 需要排在前面
        /// </summary>
        /// <returns></returns>
        public IDictionary<string, string> GetAllSimplePriceIncludes()
        {
            var query = from c in _masterDataRepository.GetAll()
                        where c.TypeCode == OutBoundConsts.MASTERDATA_TYPECODE_PRICEINCLUDE
                        orderby c.SeqNo ascending
                        select new { c.DataCode, c.DataName };

            var dicPriceInclude = query.ToDictionary(p => p.DataCode, p => p.DataName);
            return dicPriceInclude;
        }
    }
}
